﻿using System;
using VivendoByteToolkit.Enums;
using VivendoByteToolkit.ViewModels;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace VivendoByteToolkit.Controls
{
    public partial class NumberPicker : UserControl
    {
        static NumberPickerViewModel vm;

        #region Dependency properties

        #region Number dependency property
        public static int GetNumber(DependencyObject obj)
        {
            return (int)vm.NumberPicker.GetValue(NumberProperty);
        }

        public static void SetNumber(DependencyObject obj, int value)
        {
            vm.NumberPicker.SetValue(NumberProperty, value);
        }

        public static readonly DependencyProperty NumberProperty =
            DependencyProperty.RegisterAttached("Number",
            typeof(int),
            typeof(NumberPicker),
            new PropertyMetadata(0, new PropertyChangedCallback(Update)));
        #endregion

        #region Minimum dependency property
        public static int GetMinimum(DependencyObject obj)
        {
            return (int)vm.NumberPicker.GetValue(MinimumProperty);
        }

        public static void SetMinimum(DependencyObject obj, int value)
        {
            vm.NumberPicker.SetValue(MinimumProperty, value);
        }

        public static readonly DependencyProperty MinimumProperty =
            DependencyProperty.RegisterAttached("Minimum",
            typeof(int),
            typeof(NumberPicker),
            new PropertyMetadata(int.MinValue));
        #endregion

        #region Maximum dependency property
        public static int GetMaximum(DependencyObject obj)
        {
            return (int)vm.NumberPicker.GetValue(MaximumProperty);
        }

        public static void SetMaximum(DependencyObject obj, int value)
        {
            vm.NumberPicker.SetValue(MaximumProperty, value);
        }

        public static readonly DependencyProperty MaximumProperty =
            DependencyProperty.RegisterAttached("Maximum",
            typeof(int),
            typeof(NumberPicker),
            new PropertyMetadata(int.MaxValue));
        #endregion 

        #endregion

        #region Public properties
        public int Number
        {
            get
            {
                return NumberPicker.GetNumber(this);
            }
            set
            {
                NumberPicker.SetNumber(this, value);
            }
        }

        public int Minimum
        {
            get
            {
                return NumberPicker.GetMinimum(this);
            }
            set
            {
                NumberPicker.SetMinimum(this, value);
            }
        }

        public int Maximum
        {
            get
            {
                return NumberPicker.GetMaximum(this);
            }
            set
            {
                NumberPicker.SetMaximum(this, value);
            }
        }
        #endregion

        public NumberPicker()
        {
            this.InitializeComponent();
            vm = this.Resources["viewmodel"] as NumberPickerViewModel;
            vm.NumberPicker = this;
        }

        private static void Update(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != e.OldValue)
            {
                vm.Number = e.NewValue.ToString();
            }
        }
    }
}